package it.sportingnola.soccernotes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public abstract class AbstractSoccerActivity extends Activity {
    protected DbHelper dbHelper=null;
    protected Long cronaca;
    protected CharSequence team1Name;
    protected CharSequence team2Name;
    protected long default_nr_players;

    public abstract void cancella(View view);

    public void chiudi(View view) {
	finish();
    }

    private boolean controllaGiocatori() {
	if ( ! dbHelper.areThereAllPlayer(cronaca)) {
	    Toast toast=Toast.makeText(this, getString(R.string.player_missing_error), Toast.LENGTH_SHORT);
	    toast.show();
	    return false;
	}
	return true;
    }

    public abstract void doLeftSwipeAction();

    public abstract void doRigthSwipeAction();

    public void editAction(View view) {
	Intent i=new Intent(this, ActionEdit.class);
	i.putExtra(Const.Players.KEY_CRONACA, cronaca);
	i.putExtra(Const.HOME_TEAM.toString(), getTeam1Name());
	i.putExtra(Const.FOREIGN_TEAM.toString(), getTeam2Name());
	startActivityForResult(i, SoccerNotes.ACTION_EDIT);
    }

    public void editCard(View view) {
	if ( ! controllaGiocatori()) {
	    return;
	}
	Intent i=new Intent(this, CardEdit.class);
	i.putExtra(Const.Players.KEY_CRONACA, cronaca);
	i.putExtra(Const.HOME_TEAM.toString(), getTeam1Name());
	i.putExtra(Const.FOREIGN_TEAM.toString(), getTeam2Name());
	startActivityForResult(i, SoccerNotes.CARD_EDIT);
    }

    public void editGoal(View view) {
	if ( ! controllaGiocatori()) {
	    return;
	}
	Intent i=new Intent(this, GoalEdit.class);
	i.putExtra(Const.Players.KEY_CRONACA, cronaca);
	i.putExtra(Const.HOME_TEAM.toString(), getTeam1Name());
	i.putExtra(Const.FOREIGN_TEAM.toString(), getTeam2Name());
	startActivityForResult(i, SoccerNotes.GOAL_EDIT);
    }

    public void editSubst(View view) {
	if ( ! controllaGiocatori()) {
	    return;
	}
	Intent i=new Intent(this, SubstEdit.class);
	i.putExtra(Const.Players.KEY_CRONACA, cronaca);
	i.putExtra(Const.HOME_TEAM.toString(), getTeam1Name());
	i.putExtra(Const.FOREIGN_TEAM.toString(), getTeam2Name());
	startActivityForResult(i, SoccerNotes.SUBST_EDIT);
    }

    protected abstract void fillData();

    public long getCronaca() {
	return cronaca;
    }

    public DbHelper getDbHelper() {
	return dbHelper;
    }

    public CharSequence getTeam1Name() {
	return team1Name;
    }

    public CharSequence getTeam2Name() {
	return team2Name;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// recupera i parametri passati tramite Intent
	Bundle extras=getIntent().getExtras();
	if (extras != null) {
	    if (extras.getLong(Const.Players.KEY_CRONACA) != 0) {
		cronaca=extras.getLong(Const.Players.KEY_CRONACA);
	    }
	    team1Name=extras.getCharSequence(Const.HOME_TEAM.toString());
	    team2Name=extras.getCharSequence(Const.FOREIGN_TEAM.toString());
	    default_nr_players=extras.getInt(Const.Config.KEY_DEFAULT_PL_NUMBER);
	}
	if (dbHelper == null) {
	    dbHelper=new DbHelper(this);
	    dbHelper.open();
	}
	performSetTitle();
	performSetContentView();
	prepareFields();
	fillData();

    }

    /**
     * effettua il settaggio dellla content view (demandato ai figli)
     */
    protected abstract void performSetContentView();

    /**
     * effettua il settaggio del titolo (demandato ai figli)
     */
    protected abstract void performSetTitle();

    /**
     * Inizializza i campi presenti nella View
     */
    abstract protected void prepareFields();

    /**
     * resetta i campi presenti nella view
     */
    abstract protected void resetField();

    public void salva(View view) {
	saveState();
	resetField();
	fillData();
    }

    abstract protected void saveState();
}
